package org.platform.processor.diagnose.impl;

import cn.hutool.core.util.ZipUtil;
import org.apache.commons.lang3.StringUtils;
import org.platform.mo.bussmo.common.FileHeader;
import org.platform.mo.bussmo.diagnose.test.ReportCollect;
import org.platform.mo.bussmo.diagnose.test.ReportCollectContent;
import org.platform.mo.db.FdDiagnosetask;
import org.platform.processor.diagnose.IDiagnoseExecutor;
import org.platform.tool.helper.FileHelper;
import org.platform.tool.helper.ResourceHelper;
import org.platform.tool.util.FtpClientUtil;
import org.platform.tool.util.SerialNumberUtils;
import org.platform.tool.util.TimeUtils;
import org.platform.tool.util.XmlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

/**
 * 任务故障诊断
 *
 * @author FengJie
 * @date 2018/4/23
 */
@Service
public class TaskFaultDiagnoseImpl implements IDiagnoseExecutor {
    /**
     * 标识符 采集通知
     */
    private static final String IDENTIFICATION = "ReportCollect";
    /**
     * 发送方 FDS
     */
    private static final String SOURCE = "FDS";
    /**
     * 接收方 TASK
     */
    private static final String DESTINATION = "IMS";
    /**
     * 文件类型
     */
    private static final String FILE_TYPE = ".xml";

    @Autowired
    private ResourceHelper resourceHelper;
    @Autowired
    private FileHelper fileHelper;

    @Override
    public boolean doExecute(FdDiagnosetask task) {

        //todo  执行诊断流程.....

        return false;
    }

    public void generateZip() {
        String path = "F:\\微核科技\\项目\\2018-03-07 故障诊断编码设计工作\\2018-6-27接收的PDF相关文件";
        File file = ZipUtil.zip(path);
        FtpClientUtil.getInstance(resourceHelper.getFtpIp(), resourceHelper.getPtpUsername(), resourceHelper.getPtpPassword())
                .upload(file, resourceHelper.getFtpImsSendDir());
    }

    /**
     * 采集通知
     * 生成指定的xml文件
     */
    public void reportCollect() throws IOException {
        FileHeader header = new FileHeader();
        header.setCreatedTime(TimeUtils.getSystemCurrentTime());
        header.setDestination(DESTINATION);
        header.setSource(SOURCE);
        header.setIdentificationCode(IDENTIFICATION);

        ReportCollectContent content = new ReportCollectContent();
        content.setFilePath("测试");
        content.setFileType("测试");
        content.setPlanID("测试");
        content.setSubID("测试");

        ReportCollect reportCollect = new ReportCollect();
        reportCollect.setFileHeader(header);
        reportCollect.setContent(content);

        String xml = XmlUtils.beanToXml(reportCollect);
        String dir = resourceHelper.getLocalRootDir() + resourceHelper.getLocalArchivingDir();
        String time = TimeUtils.getTimeByFormat("yyyyMMddHHmmss");
        String serialnumber = SerialNumberUtils.getInstance().getSerialNumber();
        String fileName = IDENTIFICATION + "_" + SOURCE + "_" + DESTINATION + "_" + time + "_" + serialnumber;
        String file = StringUtils.join(dir, fileName, FILE_TYPE);
        //先创建文件到本地备份目录 将文件发送到ftp上
        fileHelper.writeFileContent(file, xml);
        FtpClientUtil.getInstance(resourceHelper.getFtpIp(), resourceHelper.getPtpUsername(), resourceHelper.getPtpPassword())
                .upload(file, resourceHelper.getFtpImsSendDir());
    }
}
